﻿@page "/creator/replies/{id}"
@inherits BaseComponent

<UserLayout Title="编辑回复" Class="cl-creator-form">
    <CmsCard Class="cl-posts" Title="编辑回复">
        @if (!string.IsNullOrWhiteSpace(errorMessage))
        {
            <AntAlert Type="error">@errorMessage</AntAlert>
        }
        <AntForm Id="@formId">
            <div style="font-size:1rem;font-weight:bold;margin-bottom:15px;">@model?.Title</div>
            <div class="label"><span style="color:#f00;">*</span>回答内容</div>
            <CmsMarkdown Id="@nameof(ReplyFormInfo.Content)" Value="@model?.Content" Placeholder="在这里输入回答内容" />
            <div class="button">
                <AntButton Name="发布" OnClick="@($"return AntBlazor.validate('{formId}');")" />
                <AntButton Name="取消" Type="default" OnClick="@($"location='{Url.UserRepliesUrl}';return false;")" />
            </div>
        </AntForm>
    </CmsCard>
</UserLayout>

@code {
    private ISiteService Service;
    private ReplyFormInfo model = new();
    private string formId = "reply-form";
    private string errorMessage = "";

    protected override async Task OnInitAsync()
    {
        if (CurrentUser == null)
            return;

        await base.OnInitAsync();
        Service = await CreateServiceAsync<ISiteService>();
        if (!string.IsNullOrWhiteSpace(Id))
            model = await Service.GetReplyAsync(Id);
        if (Context.Request.IsHandler(formId))
        {
            var form = Context.Request.GetModel<ReplyFormInfo>();
            model.Content = form.Content;
            var result = await Service.SaveReplyAsync(model);
            if (!result.IsValid)
                errorMessage = result.Message;
            else
                Context.Response.Redirect(Url.UserRepliesUrl.Trim('.'));
        }
    }
}